package com.jiankang.askhealth.core.area.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.jiankang.askhealth.aware.UserAware;
import com.jiankang.askhealth.base.web.PagingAction;
import com.jiankang.askhealth.core.area.domain.Area;
import com.jiankang.askhealth.core.area.service.AreaService;
import com.jiankang.askhealth.core.user.bean.UserBean;

public class AreaAction extends PagingAction implements ServletRequestAware, ServletResponseAware, UserAware {
	private String areaid;
    private String parentid;
	private String type;

	private UserBean loginUser;
	

	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private AreaService areaService;


	public String ajaxGetSelectArea() {
		List<Area> placeList;

		placeList = areaService.getAreasByParentid(parentid);

		return _commonAjaxResult(1, null, placeList);
	}

	/**
	 * 
	 * @param <T>
	 * @param response
	 * @param type
	 *            0:fail 1:success
	 * @return
	 */
	private <T> String _commonAjaxResult(int type, Object resultObj,
			List<T> placeList) {
		try {
			Map result = new HashMap();

			if (0 == type) {
				result.put("result", "fail");
			} else {
				result.put("result", "success");
			}

			if (resultObj != null) {
				result.put("resultObj", resultObj);
			}

			if (placeList != null && placeList.size() > 0) {
				result.put("resultObjs", placeList);
			}

			Gson gson = new Gson();
			String gsonStr = gson.toJson(result);

			PrintWriter out = response.getWriter();
			out.print(gsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@Override
	public void setLoginUser(UserBean userBean) {
		this.loginUser = userBean;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public String getAreaid() {
		return areaid;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}

	public String getParentid() {
		return parentid;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	
	
}
